"""
elif和else语句
学习目标：
1. 掌握elif（else if）的使用
2. 掌握else的使用
3. 理解多分支选择结构
4. 学会优化条件判断逻辑
"""

# ==================== 1. else语句 ====================
print("=" * 50)
print("1. else语句 - 二选一")
print("=" * 50)

print("语法结构：")
print("""
if 条件:
    条件为True时执行
else:
    条件为False时执行
""")

# 基本示例
age = 16
print(f"年龄：{age}岁")

if age >= 18:
    print("已成年，可以独立观影")
else:
    print("未成年，需家长陪同")

print()

# ==================== 2. else的应用场景 ====================
print("=" * 50)
print("2. else的实际应用")
print("=" * 50)

# 场景1：成绩及格判断
score = 55
print(f"成绩：{score}分")

if score >= 60:
    print("✅ 恭喜！成绩及格")
else:
    print("❌ 很遗憾，成绩不及格")
    print("建议：继续努力，争取下次考好")

print()

# 场景2：奇偶判断
number = 17
print(f"数字：{number}")

if number % 2 == 0:
    print("这是偶数")
else:
    print("这是奇数")

print()

# ==================== 3. elif语句 - 多分支选择 ====================
print("=" * 50)
print("3. elif语句 - 多分支选择")
print("=" * 50)

print("语法结构：")
print("""
if 条件1:
    条件1为True时执行
elif 条件2:
    条件1为False且条件2为True时执行
elif 条件3:
    前面都为False且条件3为True时执行
else:
    所有条件都为False时执行
""")

# 基本示例：成绩等级
score = 85
print(f"成绩：{score}分")

if score >= 90:
    grade = "优秀"
elif score >= 80:
    grade = "良好"
elif score >= 70:
    grade = "中等"
elif score >= 60:
    grade = "及格"
else:
    grade = "不及格"

print(f"等级：{grade}")

print()

# ==================== 4. elif的执行特点 ====================
print("=" * 50)
print("4. elif的执行特点（找到就停止）")
print("=" * 50)

score = 95
print(f"成绩：{score}分")
print("执行流程：")

if score >= 90:
    print("✅ 执行这里：优秀")
    print("   找到匹配条件，后面的elif和else都不执行")
elif score >= 80:
    print("这里不会执行（前面已经匹配）")
elif score >= 70:
    print("这里也不会执行")
else:
    print("这里也不会执行")

print()
print("💡 关键点：")
print("   - if-elif-else是一个整体")
print("   - 从上到下依次判断")
print("   - 找到第一个True就执行，然后跳出")
print("   - 最多执行其中一个分支")

print()

# ==================== 5. 实战：天气穿衣建议 ====================
print("=" * 50)
print("5. 实战：根据温度给穿衣建议")
print("=" * 50)

temperature = 15
print(f"当前温度：{temperature}°C")
print()

if temperature >= 30:
    suggestion = "天气炎热，穿短袖、短裤"
    item = "🩳 短裤"
elif temperature >= 25:
    suggestion = "天气温暖，穿T恤、薄外套"
    item = "👕 T恤"
elif temperature >= 15:
    suggestion = "天气凉爽，穿长袖、外套"
    item = "🧥 外套"
elif temperature >= 5:
    suggestion = "天气寒冷，穿毛衣、厚外套"
    item = "🧥 厚外套"
else:
    suggestion = "天气严寒，穿羽绒服、围巾"
    item = "🧣 羽绒服"

print(f"穿衣建议：{suggestion}")
print(f"推荐单品：{item}")

print()

# ==================== 6. 实战：BMI健康指数 ====================
print("=" * 50)
print("6. 实战：BMI健康评估")
print("=" * 50)

height = 1.75  # 米
weight = 70    # 千克
bmi = weight / (height ** 2)

print(f"身高：{height}m")
print(f"体重：{weight}kg")
print(f"BMI：{bmi:.2f}")
print()

if bmi < 18.5:
    category = "偏瘦"
    color = "🔵"
    advice = "建议增加营养摄入，适当运动"
elif bmi < 24:
    category = "正常"
    color = "🟢"
    advice = "继续保持良好的生活习惯"
elif bmi < 28:
    category = "偏胖"
    color = "🟡"
    advice = "建议控制饮食，增加运动"
else:
    category = "肥胖"
    color = "🔴"
    advice = "建议就医咨询，制定减重计划"

print(f"{color} 体重分类：{category}")
print(f"健康建议：{advice}")

print()

# ==================== 7. 实战：考试成绩分析 ====================
print("=" * 50)
print("7. 实战：考试成绩详细分析")
print("=" * 50)

student_name = "李四"
score = 78

print(f"学生：{student_name}")
print(f"成绩：{score}分")
print("-" * 50)

if score >= 90:
    print("🏆 等级：优秀")
    print("表现：非常出色！")
    print("奖励：颁发荣誉证书")
    print("建议：继续保持")
elif score >= 80:
    print("😊 等级：良好")
    print("表现：很不错！")
    print("奖励：表扬信")
    print("建议：争取更进一步")
elif score >= 70:
    print("🙂 等级：中等")
    print("表现：还可以")
    print("建议：多加练习，提高成绩")
elif score >= 60:
    print("😐 等级：及格")
    print("表现：刚刚及格")
    print("建议：需要更加努力")
else:
    print("😢 等级：不及格")
    print("表现：未达标")
    print("建议：找老师辅导，认真复习")

print()

# ==================== 8. 实战：会员等级系统 ====================
print("=" * 50)
print("8. 实战：会员等级与优惠")
print("=" * 50)

points = 3500
print(f"会员积分：{points}")
print()

if points >= 10000:
    level = "钻石会员"
    discount = 0.6  # 6折
    benefits = ["专属客服", "生日礼物", "免费配送", "优先购买", "积分翻倍"]
elif points >= 5000:
    level = "黄金会员"
    discount = 0.7
    benefits = ["专属客服", "生日礼物", "免费配送", "优先购买"]
elif points >= 2000:
    level = "白银会员"
    discount = 0.8
    benefits = ["专属客服", "生日礼物", "免费配送"]
elif points >= 500:
    level = "青铜会员"
    discount = 0.9
    benefits = ["专属客服", "生日礼物"]
else:
    level = "普通会员"
    discount = 1.0
    benefits = ["积累积分"]

print(f"🏅 等级：{level}")
print(f"💰 折扣：{int(discount * 10)}折")
print(f"🎁 权益：")
for benefit in benefits:
    print(f"   ✅ {benefit}")

print()

# ==================== 9. 实战：时间段问候 ====================
print("=" * 50)
print("9. 实战：根据时间段问候")
print("=" * 50)

hour = 14  # 24小时制

print(f"当前时间：{hour}:00")
print()

if hour < 6:
    greeting = "凌晨好，夜猫子！"
    emoji = "🌙"
    suggestion = "建议早点休息"
elif hour < 9:
    greeting = "早上好！"
    emoji = "🌅"
    suggestion = "美好的一天开始了"
elif hour < 12:
    greeting = "上午好！"
    emoji = "☀️"
    suggestion = "工作学习加油"
elif hour < 14:
    greeting = "中午好！"
    emoji = "🌤️"
    suggestion = "记得吃午饭哦"
elif hour < 18:
    greeting = "下午好！"
    emoji = "🌞"
    suggestion = "继续努力"
elif hour < 22:
    greeting = "晚上好！"
    emoji = "🌆"
    suggestion = "放松一下"
else:
    greeting = "夜深了！"
    emoji = "🌃"
    suggestion = "早点休息吧"

print(f"{emoji} {greeting}")
print(f"💡 {suggestion}")

print()

# ==================== 10. 实战：密码强度检测 ====================
print("=" * 50)
print("10. 实战：密码强度检测")
print("=" * 50)

password = "Abc123!@"
length = len(password)

print(f"密码：{'*' * length}")
print(f"长度：{length}个字符")
print()

# 计算密码强度分数
score = 0
has_lower = any(c.islower() for c in password)
has_upper = any(c.isupper() for c in password)
has_digit = any(c.isdigit() for c in password)
has_special = any(not c.isalnum() for c in password)

if length >= 8:
    score += 1
if length >= 12:
    score += 1
if has_lower:
    score += 1
if has_upper:
    score += 1
if has_digit:
    score += 1
if has_special:
    score += 1

print(f"强度分数：{score}/6")
print()

if score >= 5:
    strength = "非常强"
    color = "🟢"
    comment = "密码很安全！"
elif score >= 4:
    strength = "强"
    color = "🟡"
    comment = "密码较安全"
elif score >= 3:
    strength = "中等"
    color = "🟠"
    comment = "建议增强密码复杂度"
else:
    strength = "弱"
    color = "🔴"
    comment = "密码太弱，容易被破解"

print(f"{color} 密码强度：{strength}")
print(f"评价：{comment}")

print()

# ==================== 11. 实战：购物折扣计算 ====================
print("=" * 50)
print("11. 实战：阶梯折扣计算")
print("=" * 50)

amount = 580
is_vip = True

print(f"购物金额：¥{amount}")
print(f"VIP会员：{is_vip}")
print()

# 计算折扣
if is_vip:
    if amount >= 1000:
        discount_rate = 0.7  # VIP 1000以上7折
        reason = "VIP会员 + 消费满1000"
    elif amount >= 500:
        discount_rate = 0.75  # VIP 500以上7.5折
        reason = "VIP会员 + 消费满500"
    elif amount >= 200:
        discount_rate = 0.8  # VIP 200以上8折
        reason = "VIP会员 + 消费满200"
    else:
        discount_rate = 0.85  # VIP 8.5折
        reason = "VIP会员"
else:
    if amount >= 1000:
        discount_rate = 0.8  # 普通 1000以上8折
        reason = "消费满1000"
    elif amount >= 500:
        discount_rate = 0.85  # 普通 500以上8.5折
        reason = "消费满500"
    elif amount >= 200:
        discount_rate = 0.9  # 普通 200以上9折
        reason = "消费满200"
    else:
        discount_rate = 1.0  # 无折扣
        reason = "暂无折扣"

final_amount = amount * discount_rate
saved = amount - final_amount

print(f"折扣原因：{reason}")
print(f"折扣力度：{int(discount_rate * 10)}折")
print(f"实付金额：¥{final_amount:.2f}")
print(f"节省：¥{saved:.2f}")

print()

# ==================== 12. 实战：年龄段分类 ====================
print("=" * 50)
print("12. 实战：年龄段详细分类")
print("=" * 50)

age = 25
print(f"年龄：{age}岁")
print()

if age < 0:
    category = "年龄无效"
    stage = "错误"
    characteristics = "请输入正确的年龄"
elif age <= 2:
    category = "婴儿期"
    stage = "人生起点"
    characteristics = "快速生长发育，需要悉心照料"
elif age <= 6:
    category = "幼儿期"
    stage = "启蒙阶段"
    characteristics = "开始接触世界，培养基本能力"
elif age <= 12:
    category = "儿童期"
    stage = "成长阶段"
    characteristics = "学习知识，建立价值观"
elif age <= 18:
    category = "青少年期"
    stage = "发展阶段"
    characteristics = "身心快速发展，形成独立人格"
elif age <= 35:
    category = "青年期"
    stage = "奋斗阶段"
    characteristics = "事业起步，追求理想"
elif age <= 60:
    category = "中年期"
    stage = "成熟阶段"
    characteristics = "经验丰富，承担责任"
elif age <= 80:
    category = "老年期"
    stage = "收获阶段"
    characteristics = "享受生活，传承智慧"
else:
    category = "长寿期"
    stage = "人瑞阶段"
    characteristics = "健康长寿，令人敬仰"

print(f"分类：{category}")
print(f"阶段：{stage}")
print(f"特点：{characteristics}")

print()

# ==================== 13. 实战：星座查询 ====================
print("=" * 50)
print("13. 实战：根据日期判断星座")
print("=" * 50)

month = 7
day = 15

print(f"日期：{month}月{day}日")
print()

if (month == 3 and day >= 21) or (month == 4 and day <= 19):
    zodiac = "白羊座 ♈"
    period = "3月21日 - 4月19日"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
    zodiac = "金牛座 ♉"
    period = "4月20日 - 5月20日"
elif (month == 5 and day >= 21) or (month == 6 and day <= 21):
    zodiac = "双子座 ♊"
    period = "5月21日 - 6月21日"
elif (month == 6 and day >= 22) or (month == 7 and day <= 22):
    zodiac = "巨蟹座 ♋"
    period = "6月22日 - 7月22日"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
    zodiac = "狮子座 ♌"
    period = "7月23日 - 8月22日"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
    zodiac = "处女座 ♍"
    period = "8月23日 - 9月22日"
elif (month == 9 and day >= 23) or (month == 10 and day <= 23):
    zodiac = "天秤座 ♎"
    period = "9月23日 - 10月23日"
elif (month == 10 and day >= 24) or (month == 11 and day <= 22):
    zodiac = "天蝎座 ♏"
    period = "10月24日 - 11月22日"
elif (month == 11 and day >= 23) or (month == 12 and day <= 21):
    zodiac = "射手座 ♐"
    period = "11月23日 - 12月21日"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
    zodiac = "摩羯座 ♑"
    period = "12月22日 - 1月19日"
elif (month == 1 and day >= 20) or (month == 2 and day <= 18):
    zodiac = "水瓶座 ♒"
    period = "1月20日 - 2月18日"
else:  # (month == 2 and day >= 19) or (month == 3 and day <= 20)
    zodiac = "双鱼座 ♓"
    period = "2月19日 - 3月20日"

print(f"星座：{zodiac}")
print(f"时间段：{period}")

print()

# ==================== 14. if vs elif 对比 ====================
print("=" * 50)
print("14. 多个if vs elif的区别")
print("=" * 50)

score = 95
print(f"成绩：{score}分")
print()

print("【使用多个独立if（可能执行多个）】")
if score >= 90:
    print("  ✅ 优秀")
if score >= 80:
    print("  ✅ 良好")  # 也会执行
if score >= 60:
    print("  ✅ 及格")  # 也会执行

print()

print("【使用elif（只执行一个）】")
if score >= 90:
    print("  ✅ 优秀")
elif score >= 80:
    print("  良好")  # 不会执行
elif score >= 60:
    print("  及格")  # 不会执行

print()
print("💡 区别：")
print("   - 多个if：所有条件都会判断，可能执行多个")
print("   - elif：找到第一个True就停止，只执行一个")

print()

# ==================== 总结 ====================
print("=" * 50)
print("✨ elif和else总结")
print("=" * 50)
print("""
1. 语法结构：
   if 条件1:
       代码块1
   elif 条件2:
       代码块2
   elif 条件3:
       代码块3
   else:
       代码块4

2. 执行规则：
   ✅ 从上到下依次判断条件
   ✅ 遇到第一个True，执行对应代码块
   ✅ 执行后跳出整个结构
   ✅ 如果都是False，执行else（如果有）
   ✅ 最多执行其中一个分支

3. 使用场景：
   - 二选一：if...else
   - 多选一：if...elif...else
   - 分数等级、会员等级
   - 时间段判断
   - 范围分类

4. 注意事项：
   ⚠️ elif必须在if后面
   ⚠️ else必须在最后
   ⚠️ 条件从严格到宽松排列
   ⚠️ 互斥条件用elif，独立条件用多个if

5. 最佳实践：
   - 先判断特殊情况
   - 条件按可能性从高到低排列
   - 复杂条件可以先计算后判断
   - 使用有意义的变量名
""")
print("=" * 50)

